import axios from 'axios';

export const ImgUrl = "http://127.0.0.1:3000/imgs/"

export const UploadUrl = "http://127.0.0.1:3000/uploads/"

const instance = axios.create({
    baseURL:"http://127.0.0.1:3000",
    timeout:10000, //超时时间
    headers:{
        // 'Content-Type':'application/x-www-form-urlencoded'
        'Content-Type':'application/json'
    }
})

instance.interceptors.request.use( config => config ,e => Promise.reject(e))

instance.interceptors.response.use( resp => {
    if (resp.status === 200) {
        return Promise.resolve(resp)
    }else{
        return Promise.reject(resp)
    }
},e =>{
    if (e.response.status) {
        return Promise.reject(e.response)
    }
})

export const Http = (api,param)=>{
    return new Promise((resolve,reject)=>{
        instance.post("/api"+api,param)
            .then(res => {
                resolve(res.data)
            })
            .catch(e => {
                reject(e)
            })
    })
}

export const HttpGql = (param)=>{
    return new Promise((resolve,reject)=>{
        instance.post("/gql",param)
            .then(res => {
                resolve(res.data)
            })
            .catch(e => {
                reject(e)
            })
    })
}

export const HttpMock = (api,param)=>{
    const baseUrl = ""

    const resData = {
        code:"",
        msg:"",
        data:{}
    }

    let url = baseUrl+api
    console.log(url,param)
    return new Promise((resolve,reject)=>{
        setTimeout(()=>{
            let r = Math.random()
            if(r>0.1 && r<0.9){
                switch(api) {
                    case "/register":
                        resData.code = "ok"
                        resData.msg = "注册成功"
                        break;
                    case "/login":
                        resData.code = "ok"
                        resData.msg = "登录成功"
                        resData.data = {
                            token : "123456",
                            user:{
                                userid:"abc@mail.com",
                                username:param.name
                            }
                        }
                        break;
                    default:
                } 
                resolve(resData)
            }else{
                resData.code = "fail"
                resData.msg = "该邮箱已被注册"
                reject(resData.msg)
            }
        },1000)
    })
}